package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
	"share-chat/pkg"
	"strings"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile(c.Query("image"))
	if err != nil {
		return
	}

	etc := strings.ToLower(filepath.Ext(file.Filename))
	if !strings.Contains(".mp4.jpg.png", etc) {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "格式错误",
			"data":    "",
		})
		return
	}

	if strings.Contains(".mp4", etc) {

		if file.Size > 20*1024*1024 {
			c.JSON(http.StatusOK, gin.H{
				"code":    100000,
				"message": "视频过大",
				"data":    "",
			})
			return
		}

		upload := pkg.Upload()

		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "上传成功",
			"data":    upload,
		})

	} else {
		if file.Size > 2*1024*1024 {
			c.JSON(http.StatusOK, gin.H{
				"code":    100000,
				"message": "图片过大",
				"data":    "",
			})
			return
		}

		upload := pkg.Upload()

		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "上传成功",
			"data":    upload,
		})

	}

}
